package com.sxau.software_educational_system_backend.aspect;

import com.sxau.software_educational_system_backend.common.BaseResponse;
import com.sxau.software_educational_system_backend.common.ErrorCode;
import com.sxau.software_educational_system_backend.common.ResultUtils;
import com.sxau.software_educational_system_backend.context.BaseContext;
import com.sxau.software_educational_system_backend.domain.entity.Admin;
import com.sxau.software_educational_system_backend.exception.BusinessException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import static com.sxau.software_educational_system_backend.utils.PasswordUtils.hashPassword;

@Aspect
@Component
public class AdminOnlyAspect {

    @Before("@annotation(com.sxau.software_educational_system_backend.annotation.AdminOnlyTwo)")
    public void checkAdminAccessTwo(JoinPoint joinPoint) {
        Admin admin = BaseContext.getCurrentUser();
        System.out.println("当前用户类型：" + admin.getRole());
        System.out.println("当前用户对象：" + admin);
        if (admin == null || !admin.getRole().toString().equals("2")) {

            throw new BusinessException(ErrorCode.NULL_ERROR);

    }}


    @Before("@annotation(com.sxau.software_educational_system_backend.annotation.AdminOnlyOne)")
    public void checkAdminAccessOne(JoinPoint joinPoint) {
        Admin admin = BaseContext.getCurrentUser();
        System.out.println("当前用户类型：" + admin.getRole());
        System.out.println("当前用户对象：" + admin);
        if (admin == null || !admin.getRole().toString().equals("1")) {
        throw new BusinessException(ErrorCode.NULL_ERROR);
        }
    }

    @Before("@annotation(com.sxau.software_educational_system_backend.annotation.AdminOneOrTwo)")
    public void checkAccess(JoinPoint joinPoint) {
        System.out.println("进入切面");
        Admin admin = BaseContext.getCurrentUser();
        System.out.println("当前用户类型：" + admin.getRole());
        System.out.println("当前用户对象：" + admin);
        if (admin == null) throw new BusinessException(ErrorCode.NULL_ERROR);


        int role = admin.getRole();
        if (role != 1 && role != 2) { // 允许1或2
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
    }
}
